using System;
using Symbonix.Library.Mail;
using System.Web;
using System.Web.Caching;
using Symbonix.NorSoNet.Framework.DAL.Repositories;
using Symbonix.NorSoNet.Framework.DAL.Entities;
namespace Symbonix.NorSoNet.Framework.BLL.Configuration {
	
	public class SingleInstanceSettings : SettingsBase  {

        public override int Id
        {
            get;
            protected set;
        }

        public override string SiteName
        {
            get;
            set;
        }

        public override string SiteUrl
        {
            get;
            set;
        }

        public override string AdminTheme
        {
            get;
            set;
        }

        public override string UserDefaultTheme
        {
            get;
            set;
        }

        public override string AdminTimeZone
        {
            get;
            set;
        }

        public override string UserDefaultTimeZone
        {
            get;
            set;
        }

        public override string SiteMailSenderName
        {
            get;
            set;
        }

        public override string AdminEmail
        {
            get;
            set;
        }

        public override string SupportEmail
        {
            get;
            set;
        }

        public override int MaxInactiveRegisterCount
        {
            get;
            set;
        }

        public override MailSettings SiteMailSettings
        {
            get;
            set;
        }

        protected SingleInstanceSettings() { }

        protected internal static ISettings GetInstance(){ return new SingleInstanceSettings();}

        public override SingleInstanceSettings Load()
        {
            SingleInstanceSettings settings = (SingleInstanceSettings)HttpContext.Current.Cache["Settings"];
            if (settings == null)
            {
                SettingDataRepository settingsRepository = new SettingDataRepository();
                int totalItems = 0;
                SettingData setting = (SettingData)settingsRepository.GetAll("SettingID", true, true, 1, 10, out totalItems)[0];
                if (setting != null)
                {
                    MailSettings mailSettings = new MailSettings();
                    mailSettings.Password = setting.SmtpPassword;
                    mailSettings.Port = setting.SmtpPort;
                    mailSettings.ServerAddress = setting.SmtpServerAddress;
                    mailSettings.UserName = setting.SmtpUsername;
                    mailSettings.UseSSL = setting.SmtpUseSSL;

                    settings = new SingleInstanceSettings();
                    settings.Id = setting.SettingID;
                    settings.AdminTheme = setting.AdminTheme;
                    settings.AdminTimeZone = setting.AdminTimeZone;
                    settings.SiteName = setting.SiteName;
                    settings.SiteMailSettings = mailSettings;
                    settings.SiteUrl = setting.SiteUrl;
                    settings.UserDefaultTheme = setting.UserDefaultTheme;
                    settings.UserDefaultTimeZone = setting.UserDefaultTimeZone;
                    settings.SiteMailSenderName = setting.SmtpSenderName;
                    settings.MaxInactiveRegisterCount = setting.MaxInactiveRegisterCount;
                    settings.AdminEmail = setting.AdminEmail;
                    settings.SupportEmail = setting.SupportEmail;

                    HttpContext.Current.Cache.Insert("Settings", settings, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
                }
            }

            return settings;
        }
    }

}
